package org.spring.springboot.app.sys.area.controller;

import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.spring.springboot.app.sys.area.domain.vo.*;
import org.spring.springboot.app.base.R;
import org.spring.springboot.app.base.Type;
import org.spring.springboot.app.sys.area.domain.po.SysAreaPO;
import org.spring.springboot.app.sys.area.service.ISysAreaService;
import org.spring.springboot.component.annotation.AccessTokenVerify;
import org.spring.springboot.util.ApiIndex;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Api(description="区域操作接口",tags = ApiIndex.SysAreaController)
@RequestMapping(value = "/api/sysArea")
@RestController
public class SysAreaController {

    @Autowired
    private ISysAreaService sysAreaService;

    @ApiOperation(value = "根据上级查询区域列表")
    @RequestMapping(value = "/findByParent", method = RequestMethod.GET)
    public R<List<SysAreaVO>> findByParent(
            @ApiParam(value = "上级id", required = true) @RequestParam(defaultValue = "0") String parentId
    ) {
        R<List<SysAreaVO>> result = new R<List<SysAreaVO>>();
        List<SysAreaVO> list = sysAreaService.findByParent(parentId);
        result.setData(list);
        return result;
    }

    private List<SysAreaVO> getSysAreaVOS(List<SysAreaPO> _list) {
        List<SysAreaVO> list = new ArrayList<SysAreaVO>();
        if (_list != null && _list.size()>0) {
            for (SysAreaPO po : _list) {
                SysAreaVO vo = new SysAreaVO();
                BeanUtils.copyProperties(po,vo);
                list.add(vo);
            }
        }
        return list;
    }

    @ApiOperation(value = "查询区域列表-所有")
    @RequestMapping(value = "/queryAll", method = RequestMethod.GET)
    public R<List<SysAreaVO>> queryAll() {
        R result = new R();
        List<SysAreaPO> _list = sysAreaService.findAll();
        List<SysAreaVO> list = getSysAreaVOS(_list);
        result.setData(list);
        return result;
    }

    @ApiOperation(value = "查询列表-所有-mini")
    @RequestMapping(value = "/query_all_mini", method = RequestMethod.GET)
    public R<List<SysAreaQueryMiniVO>> queryAllMini(
            @ApiParam(value = "最小类型级别（例如1则查询中国以下）", required = true)@RequestParam(defaultValue = "1") int typeMin,
            @ApiParam(value = "最大类型级别（例如4则查询区级及以上）", required = true)@RequestParam(defaultValue = "4") int typeMax,
            @ApiParam(value = "区域id", required = false)@RequestParam(defaultValue = "") String areaId,
            @ApiParam(value = "名称", required = false)@RequestParam(defaultValue = "") String name
    ) {
        R result = new R();
        SysAreaQueryMiniSearchVO searchVO = new SysAreaQueryMiniSearchVO();
        searchVO.setTypeMin(typeMin);
        searchVO.setTypeMax(typeMax);
        searchVO.setAreaId(areaId);
        searchVO.setName(name);
        List<SysAreaQueryMiniVO> _list = sysAreaService.findAllMini(searchVO);
        result.setData(_list);
        return result;
    }

    @ApiOperation(value = "查询区域列表-分页")
    @RequestMapping(value = "", method = RequestMethod.GET)
    public R<PageInfo<List<SysAreaVO>>> queryPage(
            @ApiParam(value = "页数", required = true) @RequestParam(defaultValue = "1") int pageNum,
            @ApiParam(value = "每页条数", required = true) @RequestParam(defaultValue = "10") int pageSize
    ) {
        R<PageInfo<List<SysAreaVO>>> result = new R<PageInfo<List<SysAreaVO>>>();
        List<SysAreaPO> _list = sysAreaService.findPage(pageNum,pageSize);
        List<SysAreaVO> list = getSysAreaVOS(_list);
        result.setData(new PageInfo(list));
        return result;
    }

    @ApiOperation(value = "根据Id获取单个信息")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public R<SysAreaVO> find(
            @ApiParam(value = "区域id", required = true) @PathVariable("id") String id
    ) {
        R<SysAreaVO> result = new R<SysAreaVO>();
        SysAreaPO po = sysAreaService.find(id);
        if(po==null){
            result.setType(Type.OperError);
            result.setMessage("未查询到记录!");
            return result;
        }
        SysAreaVO vo = new SysAreaVO();
        BeanUtils.copyProperties(po,vo);
        result.setData(vo);
        return result;
    }

    @ApiOperation(value = "保存区域信息")
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    @AccessTokenVerify
    @RequestMapping(value = "", method = RequestMethod.POST)
    public R save(
            @ApiParam(value = "区域对象", required = true) @RequestBody SysAreaSaveInVO area
    ) {
        R result = new R();
        return  sysAreaService.saveArea(area);
    }


    @ApiOperation(value = "修改区域信息")
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    @AccessTokenVerify
    @RequestMapping(value = "", method = RequestMethod.PUT)
    public R update(
            @ApiParam(value = "区域对象", required = true) @RequestBody SysAreaUpdateInVO area
    ) {
        R result = new R();
        return sysAreaService.updateArea(area);
    }

    @ApiOperation(value = "删除区域信息")
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    @AccessTokenVerify
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public R delete(
            @ApiParam(value = "区域id", required = true) @PathVariable("id") String id
    ) {
        R result = new R();
        int code = sysAreaService.delete(id);
        if (code == 0) {
            result.setType(Type.OperError);
            result.setMessage("删除失败");
        }
        return result;
    }
}
